package com.my.system.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.my.common.core.domain.Result;
import com.my.common.core.enums.ResultCode;
import com.my.system.controller.LoginResult;
import com.my.system.domain.SysUser;
import com.my.system.mapper.SysUserMapper;
import com.my.system.service.ISysUserService;
import com.my.system.utils.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public Result<Void> login(String userAccount, String password) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //select password from tb_sys_user where user_account = #{userAccount}
       SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper.select(SysUser::getPassword)
                .eq(SysUser::getUserAccount,userAccount));
       //    Result loginResult = new Result();

        if(sysUser == null){

//            loginResult.setCode(ResultCode.FAILED_USER_NOT_EXISTS.getCode());
//            loginResult.setMessage(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(BCryptUtils.matchesPassword(password,sysUser.getPassword())){
//            loginResult.setCode(ResultCode.SUCCESS.getCode());
//            loginResult.setMessage(ResultCode.SUCCESS.getMessage());
            return  Result.ok();
        }
//        loginResult.setCode(ResultCode.FAILED_LOGIN.getCode());
//        loginResult.setMessage(ResultCode.FAILED_LOGIN.getMessage());

        return  Result.fail(ResultCode.FAILED_LOGIN);
    }
}
